package com.hishop.common.filter;

import com.hishop.common.Constant;
import com.hishop.pojo.User;
import com.hishop.util.CookieUtil;
import com.hishop.util.JsonUtil;
import com.hishop.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by hejie on 2018/3/11.
 */
public class SessionExpireFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest)request;
        String loginToken= CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isNotBlank(loginToken)){
            String userJson= RedisShardedPoolUtil.get(loginToken);
            User user= JsonUtil.string2Obj(userJson,User.class);
            if (user != null){
                //重置session时间
                RedisShardedPoolUtil.expire(loginToken, Constant.RedisCacheExtime.REDIS_SESSION_EXTIME);
            }
        }
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
